
const db = wx.cloud.database()

Page({
  data:{
    recipeList:[],//菜谱列表变量
    page:1,
    pagesize:3,
    isMore:true,//是否有下页更多
    keywords:'',//用户输入的关键字
  },
  onLoad(options){
    console.log(options)
    this.data.keywords = options.keywords || '红烧'
    wx.setNavigationBarTitle({
      title:this.data.keywords + '搜索结果'
    })
    this._getRecipeList()
  },
  onReachBottom(){
    this.data.page++
    this._getRecipeList()
  },
  //获取用户输入的关键字进行搜索，模糊匹配菜谱表中recipeName字段
  async _getRecipeList(){

    if( !this.data.isMore ){
      return
    }
      wx.showLoading({
        title: '正在请求...',
      })
      let keywords = this.data.keywords
      let page = this.data.page 
      let pagesize = this.data.pagesize
      let skip = (page-1)*pagesize
      let recipesResult = await db.collection('re-recipes').where({
        recipeName:db.RegExp({
            regexp:keywords,options:'i'
        })
      }).limit(pagesize).skip(skip).orderBy( 'addtime','desc' ).get()
      console.log(recipesResult,'aaa')
      if( recipesResult.data.length == 0 ){
        this.setData({
          isMore:false
        })
      }
      wx.hideLoading()
      this.setData({
        recipeList:this.data.recipeList.concat( recipesResult.data  )
      })
  }
})